package com.it.sun.esaychat.chat.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.it.sun.easychat.model.util.ResultHelper;
import com.it.sun.easychat.model.vo.ResultVO;
import com.it.sun.esaychat.chat.entity.ChatRoomEntity;
import com.it.sun.esaychat.chat.service.IChatRoomServices;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author sunmannian
 * @since 2020-05-27
 */
@RestController
@RequestMapping("/chatRoom")
@Api(tags="聊天室控制器")
public class ChatRoomController {

    @Autowired
    private IChatRoomServices chatRoomServices;

    @PostMapping
    @ApiOperation("新建聊天室")
    public ResultVO<Void> save(@RequestBody ChatRoomEntity entity) {
        chatRoomServices.save(entity);
        return ResultHelper.success();
    }


    @PutMapping
    @ApiOperation("更新聊天室信息")
    public ResultVO<Void> update(@RequestBody ChatRoomEntity entity) {
        chatRoomServices.updateById(entity);
        return ResultHelper.success();
    }

    @DeleteMapping
    @ApiOperation("删除聊天室")
    public ResultVO<Void> delete(@RequestBody ChatRoomEntity entity) {
        chatRoomServices.removeById(entity);
        return ResultHelper.success();
    }

    @GetMapping("/{id}")
    @ApiOperation("根据id查看聊天室信息")
    public ResultVO<Void> get(@PathVariable("id") String id) {
        chatRoomServices.getById(id);
        return ResultHelper.success();
    }

    @GetMapping("")
    @ApiOperation("获取聊天室列表")
    public ResultVO<List<ChatRoomEntity>> list() {
        return ResultHelper.getSuccess(chatRoomServices.list());
    }

    @GetMapping("/page/{currentPage}/{pageSize}")
    @ApiOperation("分页获取聊天室信息")
    public ResultVO<IPage<ChatRoomEntity>> page(
            @PathVariable("currentPage") long currentPage,
            @PathVariable("pageSize") long pageSize) {
        IPage<ChatRoomEntity> page = new Page<>(currentPage, pageSize);
        return ResultHelper.getSuccess(chatRoomServices.page(page));
    }
}

